define(function(require, exports){
  'use strict';
  var _const = require('seajs/fn/fnConst');
  var fnUtils = require('seajs/fn/fnUtils');
  var py = require('py');
 /**
  * [{
  *       groupId: 1,
  *       groupName: "西湖中队一支队",
  *       isSelected: false,
  *       totalNum: 10,
  *       onlineNum: 2,
  *       members: [
  *       {
  *          memberId: 1,
  *          memberName: "张三",
  *          position:[120.153576, 30.387459],
  *          category: 1,
  *          status: 1,
  *          inGid: 2,
  *          positionTime: 1234567,
  *          inGroupId:1
  *       },
  *       {
  *          memberId: 2,
  *          memberName: "李四",
  *          position:[120.154576, 30.287459],
  *          category: 2,
  *          status: 3,
  *          inGid: 2,
  *          positionTime: 1234567,
  *          inGroupId:1
  *       },
  *       {
  *          memberId: 3,
  *          memberName: "王五",
  *          position:[120.155576, 30.587459],
  *          category: 2,
  *          status: 2,
  *          inGid: 2,
  *          positionTime: 1234567,
  *          inGroupId:1
  *       }
  *     ]
  * }]
  */
  var formatGroup = function(uid, _group, defaultGid){
    var group = undefined;
    if(_group){
      group = {};
      group['groupId'] = parseInt(_group['groupId']);
      group['groupName'] = _group['groupName']; 
      group['status'] = _group['status']; 
      if(parseInt(defaultGid) === group['groupId']){
        group['isSelected'] = true;
      }else{
        group['isSelected'] = false;
      }
      var _members = _group['members'];
      if(_members instanceof Array){
        group['totalNum'] =  _members.length-1;
        group['onlineNum'] = -1;
        group['members'] = {};

        var onLineMembers = [], otherOnLineMembers = [], offLineMembers = [];

        for(var i = 0; i < _members.length; i++){
          var member = {}, _member = _members[i];
          member['memberId'] = parseInt(_member['userId']);
          if(member['memberId'] !== uid){
            member['memberCode'] = _member['userCode'];
            member['memberName'] = _member['userName'];
            member['py'] = py.makePy(member['memberName']).toUpperCase();
            member['category'] = parseInt(_member['userType']);
            member['inGroupId'] = _member['currGroupId'];
            member['inGroupName'] = _member['currGroupName'];
            member['position'] = [fnUtils.formatDouble(
                isNaN(_member['position']['lng']) === false ? _member['position']['lng'] : 116.4, 
                6
              ), 
              fnUtils.formatDouble(
                isNaN(_member['position']['lat']) === false ? _member['position']['lat'] : 39.9 , 
                6
              )];
            member['positionTime'] = _member['position']['createtime'] === undefined? 
            undefined : fnUtils.unixTimeFormat(Number(_member['position']['createtime']));
            if(_member['onlineFlag'] == _const.IS_ON_LINE.ON_LINE){
              group['onlineNum']++;
              member['status'] = _const.IS_ON_LINE.ON_LINE;
              if(group['groupId'] !== _member['currGroupId']){
                otherOnLineMembers.push(member);
              }else{
                onLineMembers.push(member);
              }
            }else{
              member['status'] = _const.IS_ON_LINE.OFF_LINE;
              offLineMembers.push(member);
            }
          }else{
            group['onlineNum']++;
          }
        }
        group['members']['online'] = fnUtils.memberSort(onLineMembers);  
        group['members']['inOther'] = fnUtils.memberSort(otherOnLineMembers);  
        group['members']['offline'] = fnUtils.memberSort(offLineMembers); 
      }
    }
    return group;
  }

  var formatData = function(json, uid, defaultGid){
	  var body = json.body;
	  var data = [];
    if(body instanceof Array){
      for(var i = 0; i < body.length; i++){
        data.push(formatGroup(uid, body[i], defaultGid));
      }
    }
    return data;
  }

  // {
  // companyId:148,
  // createDatetime: "2016-09-19 17:21:04",
  // currGroupId: null,
  // defaultGroupId: 529,
  // imei:"",
  // level: 0,
  // mobile: "13333333331",
  // onlineFlag: 0,
  // position: Object,
  // sex: 0,
  // status: 1,
  // userCode: "zc00022",
  // userId: 332308,
  // userName: "研发1",
  // userType:3
  // }
  var formatMember = function(json, gid){
    var _member = json.body;
    var member = {};
    member['memberId'] = _member['userId'];
    member['memberName'] = _member['userName'];
    member['py'] = py.makePy(member['memberName']).toUpperCase();
    member['category'] = parseInt(_member['userType']);
    member['inGroupId'] = _member['currGroupId'];
    if(_member['onlineFlag'] == _const.IS_ON_LINE.ON_LINE){
      member['status'] = _const.IS_ON_LINE.ON_LINE;
    }else{
      member['status'] = _const.IS_ON_LINE.OFF_LINE;
    }
    member['position'] = [fnUtils.formatDouble(
        isNaN(_member['position']['lng']) === false ? _member['position']['lng'] : 116.4, 
        6
      ), 
      fnUtils.formatDouble(
        isNaN(_member['position']['lat']) === false ? _member['position']['lat'] : 39.9 , 
        6
      )];
    member['positionTime'] = _member['position']['createtime'] === undefined? 
      undefined : fnUtils.unixTimeFormat(Number(_member['position']['createtime']));
    return member;
  }

  exports.formatGroup = formatGroup,
  exports.formatData = formatData,
  exports.formatMember = formatMember

});